package com.wave.olap;

import java.util.Comparator;
import java.util.List;

public class MultiDimensionalKey<T> implements Comparator<MultiDimensionalKey<T>>, Comparable<MultiDimensionalKey<T> > {
	private List<T> keys = null;
	public MultiDimensionalKey(List<T> keys) {
		this.keys = keys;
	}
	
	@SuppressWarnings("unchecked")
	public boolean equals(Object obj) {
		MultiDimensionalKey<T> ol = (MultiDimensionalKey<T>)obj;
		if (keys.size() != ol.keys.size())
			return false;
		for (int i = 0; i < keys.size(); i++) 
			if (!keys.get(i).equals(ol.keys.get(i)))
				return false;
		return true;
	}

	public int compare(MultiDimensionalKey<T> o1,MultiDimensionalKey<T> o2) {
		MultiDimensionalKey<T> key1 = (MultiDimensionalKey<T>)o1;
		MultiDimensionalKey<T> key2 = (MultiDimensionalKey<T>)o2;
		if (key1.keys.size() != key2.keys.size())
			if (key1.keys.size() < key2.keys.size())
				return -1;
			else
				return 1;
		for (int i = 0; i < key1.keys.size(); i++)
			if (key1.keys.get(i).equals(key2.keys.get(i)))
				return 0;
		return -1;
	}

	public int compareTo(MultiDimensionalKey<T> k) {
		for (int i = 0; i < keys.size(); i++) 
			if (!keys.get(i).equals(k.keys.get(i)))
				return -1;
		return 0;
	}	
	
    public int hashCode() {
    	String s = new String();
    	for (int i = 0; i < keys.size(); i++) 
    		s+= keys.get(i);
    	return s.hashCode();
    }
}
